package middleware

import (
	"fmt"
	"net/http"
	"project/serializer"
	"project/tool/jwt"

	"github.com/gin-gonic/gin"
)

func SignCheck() gin.HandlerFunc {
	return func(c *gin.Context) {
		ip := c.ClientIP()

		fmt.Println(ip)

		token := c.GetHeader("token")
		if token == "" {
			token = c.DefaultQuery("token", "")
		}
		if token == "" {
			c.JSON(http.StatusOK, serializer.Err(401, "invalid token", nil))
			c.Abort()
			return
		}

		claims, err := jwt.ParseToken(token)
		if err != nil {
			c.JSON(http.StatusOK, serializer.Err(401, "invalid token", err))
		}

		userAccount := claims.UserAccount
		fmt.Println(userAccount)

		c.Next()

	}
}
